var COMMON_CONFIGS = {
	TIMEOUT_MSG : '会话已超时，请重新登录',
	UNAUTHORIZED_MSG : '没有操作权限，请联系管理员',
	LOAD_MSG : '操作中...',
	ALERT_TITLE : '系统提示',
	CONFIRM_TITLE : '系统提示',
	EASYUI_WIDTH : '100%',
	EASYUI_HEIGHT : 34,
	EASYUI_POSITION : 'bottom'
};

$.ajaxSetup({
	type : 'post',
	dataType : 'json',
	complete : function(xhr, status) {
		if (xhr.status == 401) {
			$.error(COMMON_CONFIGS.UNAUTHORIZED_MSG);
		} else if (xhr.status == 408) {
			$.error(COMMON_CONFIGS.TIMEOUT_MSG, function() {
				top.location = xhr.responseText;
			});
		}
	}
});

$.ajaxSettings.traditional = true;

layer.config({
	skin : 'layui-layer-admin',
	resize : false,
	btnAlign : 'c',
	move : false
});

$.extend({
	mask : function() {
		$.messager.progress({
			msg : COMMON_CONFIGS.LOAD_MSG
		});
	},
	unmask : function() {
		$.messager.progress("close");
	},
	handle : function(data, callback1, callback2) {
		if (data.success) {
			$.success(data.msg, callback1);
		} else {
			$.error(data.msg, callback2);
		}
	},
	open : function(option) {
		var option = option || {};
		if (!option.width) {
			option.width = ($(window).width() - 40);
		}
		if (!option.height) {
			option.height = ($(window).height() - 40);
		}
		layer.open({
			type : 2,
			title : option.title,
			content : option.url,
			area : [ option.width + 'px', option.height + 'px' ],
			end : option.end
		})
	},
	close : function() {
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	},
	info : function(text, yes) {
		layer.alert(text, {
			title : COMMON_CONFIGS.ALERT_TITLE,
			closeBtn : 0,
			icon : 0
		}, function(index) {
			if (typeof yes == 'function') {
				yes();
			}
			layer.close(index);
		});
	},
	success : function(text, yes) {
		layer.alert(text, {
			title : COMMON_CONFIGS.ALERT_TITLE,
			closeBtn : 0,
			icon : 1
		}, function(index) {
			if (typeof yes == 'function') {
				yes();
			}
			layer.close(index);
		});
	},
	error : function(text, yes) {
		layer.alert(text, {
			title : COMMON_CONFIGS.ALERT_TITLE,
			closeBtn : 0,
			icon : 2
		}, function(index) {
			if (typeof yes == 'function') {
				yes();
			}
			layer.close(index);
		});
	},
	confirm : function(text, yes, cancel) {
		layer.confirm(text, {
			title : COMMON_CONFIGS.CONFIRM_TITLE,
			closeBtn : 0,
			icon : 3
		}, function(index) {
			if (typeof yes == 'function') {
				yes();
			}
			layer.close(index);
		}, function(index) {
			if (typeof cancel == 'function') {
				cancel();
			}
			layer.close(index);
		});
	},
	prompt : function(title, yes) {
		layer.prompt({
			skin : 'layui-layer-admin',
			title : title
		}, function(value, index, elem) {
			if (typeof yes == 'function') {
				yes(value);
			}
			layer.close(index);
		});
	},
	toTree : function(data) {
		var treeData = [], temp = [];
		for ( var i in data) {
			temp[data[i]['id']] = data[i];
		}
		for ( var i in data) {
			if (temp[data[i]['pid']] && data[i]['id'] != data[i]['pid']) {
				if (!temp[data[i]['pid']]['children']) {
					temp[data[i]['pid']]['children'] = [];
				}
				temp[data[i]['pid']]['children'].push(data[i]);
			} else {
				treeData.push(data[i]);
			}
		}
		return treeData;
	},
	toObject : function(data, attr1, attr2) {
		var objectData = {};
		for ( var i in data) {
			objectData[data[i][attr1]] = data[i][attr2];
		}
		return objectData;
	},
	toOption : function(data) {
		return $.toObject(data, 'value', 'text');
	},
	loadJSON : function(url) {
		return $.ajax({
			url : url,
			async : false
		}).responseJSON;
	},
	loadOption : function(url) {
		return $.toOption($.loadJSON(url));
	},
	loadTree : function(url) {
		return $.toObject($.loadJSON(url), 'id', 'text');
	},
	date : function() {
		var date = new Date();
		return date.toFormat('YYYY-MM-DD');
	},
	datetime : function() {
		var date = new Date();
		return date.toFormat('YYYY-MM-DD HH24:MI:SS');
	},
	serverDate : function() {
		var date = new Date($.ajax({
			async : false
		}).getResponseHeader("Date"));
		return date.toFormat('YYYY-MM-DD');
	},
	serverDatetime : function() {
		var date = new Date($.ajax({
			async : false
		}).getResponseHeader("Date"));
		return date.toFormat('YYYY-MM-DD HH24:MI:SS');
	}
});
$.fn.extend({
	serializeObject : function() {
		var objejct = {};
		var array = this.serializeArray();
		for ( var i in array) {
			objejct[array[i]['name']] = array[i]['value'];
		}
		return objejct;
	}
});